package com.yixia.system.mapper.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yixia.common.core.domain.entity.SysUser;
import com.yixia.common.utils.StringUtils;
import com.yixia.system.domain.system.SysOperLog;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.cache.Cache;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * @author 35038
 * @description 针对表【sys_oper_log(操作日志记录)】的数据库操作Mapper
 * @createDate 2023-08-03 09:23:50
 * @Entity com.yixia.entity.SysOperLog
 */
@Mapper
public interface SysOperLogMapper extends BaseMapper<SysOperLog> {

    /**
     * 添加日志信息
     *
     * @param operLog
     * @return
     */
    default int insertOperlog(SysOperLog operLog) {
        int insert = this.insert(operLog);
        return insert;
    }

    /**
     * 进行分页条件查询
     */
    default Page<SysOperLog> selectOperLogListPage(Page<SysOperLog> page, SysOperLog operLog) {
        LambdaQueryWrapper<SysOperLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(operLog.getTitle()), SysOperLog::getTitle, operLog.getTitle())
                .like(StringUtils.isNotEmpty(operLog.getOperName()), SysOperLog::getOperName, operLog.getOperName())
                .eq(StringUtils.isNotEmpty(operLog.getBusinessType()), SysOperLog::getBusinessType, operLog.getBusinessType())
                .eq(StringUtils.isNotEmpty(operLog.getStatus()), SysOperLog::getStatus, operLog.getStatus());

        if (StringUtils.isNotEmpty(operLog.getBusinessTypes())) {
            List<String> collect = Arrays.stream(operLog.getBusinessTypes()).map(Object::toString).collect(Collectors.toList());
            queryWrapper.in(SysOperLog::getBusinessType, collect);
        }
        // 时间
        queryWrapper.gt(StringUtils.isNotEmpty(operLog.getBeginTime()), SysOperLog::getBeginTime, operLog.getBeginTime())
                .lt(StringUtils.isNotEmpty(operLog.getEndTime()), SysOperLog::getEndTime, operLog.getEndTime());

        queryWrapper.orderByDesc(SysOperLog::getOperId);
        // 进行分页查询
        return this.selectPage(page, queryWrapper);

    }

    /**
     * 进行分页条件查询
     */
    default List<SysOperLog> selectOperLogList(SysOperLog operLog) {

        LambdaQueryWrapper<SysOperLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(operLog.getTitle()), SysOperLog::getTitle, operLog.getTitle())
                .like(StringUtils.isNotEmpty(operLog.getOperName()), SysOperLog::getOperName, operLog.getOperName())
                .eq(StringUtils.isNotEmpty(operLog.getBusinessType()), SysOperLog::getBusinessType, operLog.getBusinessType())
                .eq(StringUtils.isNotEmpty(operLog.getStatus()), SysOperLog::getStatus, operLog.getStatus());

        if (StringUtils.isNotEmpty(operLog.getBusinessTypes())) {
            List<String> collect = Arrays.stream(operLog.getBusinessTypes()).map(Object::toString).collect(Collectors.toList());
            queryWrapper.in(SysOperLog::getBusinessType, collect);
        }
        // 时间
        queryWrapper.gt(StringUtils.isNotEmpty(operLog.getBeginTime()), SysOperLog::getOperTime, operLog.getBeginTime())
                .lt(StringUtils.isNotEmpty(operLog.getEndTime()), SysOperLog::getOperTime, operLog.getEndTime());

        queryWrapper.orderByDesc(SysOperLog::getOperId);
        List<SysOperLog> sysOperLogs = this.selectList(queryWrapper);
        return sysOperLogs;
    }
}




